package com.ly.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.reggie.common.R;
import com.ly.reggie.dto.DishDto;
import com.ly.reggie.dto.SetmealDto;
import com.ly.reggie.pojo.Category;
import com.ly.reggie.pojo.Dish;
import com.ly.reggie.pojo.DishFlavor;
import com.ly.reggie.service.CategoryService;
import com.ly.reggie.service.DishFlavorService;
import com.ly.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    DishService dishService;

    @Autowired
    DishFlavorService dishFlavorService;

    @Autowired
    CategoryService categoryService;


    /**
     * 新增菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());

        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }


    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //分页构造器
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);

        //查询条件构造器
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加分页条件
        dishLambdaQueryWrapper.like(name != null, Dish::getName, name);
        //添加排序条件
        dishLambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);

        dishService.page(pageInfo, dishLambdaQueryWrapper);

        //对象拷贝,将pageInfo拷贝到pageDto中，但是排除records属性
        BeanUtils.copyProperties(pageInfo, dishDtoPage, "records");

        //获取Dish的records属性
        List<Dish> records = pageInfo.getRecords();
        //使用lambada表达式转换dishDto中的records属性
        List<DishDto> dishDtoRecords = (records).stream().map((item) -> {
            DishDto dishDto = new DishDto();
            //将dish中的属性复制到dishDto中
            BeanUtils.copyProperties(item, dishDto);
            //获取Dish类中的categoryId属性
            Long categoryId = item.getCategoryId();
            //根据categoryId属性使用categoryService类查找菜品的category
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //根据category类中的getName方法找到categoryName
                String categoryName = category.getName();
                //将categoryName赋给dishDto中的CategoryName属性
                dishDto.setCategoryName(categoryName);
            }
            //返回新构造的dishDto
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(dishDtoRecords);

        return R.success(dishDtoPage);
    }


    /**
     * 根据id查询菜品信息和口味信息，展示在修改页面
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> showDish(@PathVariable Long id) {
        DishDto byIdWithFlavor = dishService.getByIdWithFlavor(id);

        return R.success(byIdWithFlavor);
    }


    /**
     * 保存修改后的菜品信息
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> changeDish(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());

        dishService.updateWithFlavor(dishDto);

        return R.success("修改成功");
    }


    /**
     * 启售，批量启售
     *
     * @param ids
     * @return
     */

    @PostMapping("/status/1")
    public R<String> openStatus(Long[] ids) {

        for (Long id : ids
        ) {
            Dish dish = dishService.getById(id);
            dish.setStatus(1);
            dishService.updateById(dish);
        }

        return R.success("修改成功");
    }


    /**
     * 停售，批量停售
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> closeStatus(Long[] ids) {

        for (Long id : ids
        ) {
            Dish dish = dishService.getById(id);
            dish.setStatus(0);
            dishService.updateById(dish);
        }

        return R.success("修改成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteByIds(Long[] ids) {

        for (Long id : ids
        ) {
            Dish dish = dishService.getById(id);
            Integer status = dish.getStatus();
            if (status == 0){
                dishService.removeById(id);
            }else {
                return R.error("菜品正在售卖无法删除");
            }

        }

        return R.success("删除成功");
    }


//    /**
//     * 根据条件查询菜品数据
//     *
//     * @param dish
//     * @return
//     */
//    @GetMapping("/list")
//    public R<List<Dish>> addSetmeal(Dish dish) {
//
//        //添加查询条件
//        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
//        lambdaQueryWrapper.eq(Dish::getStatus,1);
//        //添加排序条件
//        lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
//
//        List<Dish> list = dishService.list(lambdaQueryWrapper);
//        return R.success(list);
//    }


    /**
     * 根据条件查询菜品数据
     *
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> addSetmeal(Dish dish) {

        //添加查询条件
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        lambdaQueryWrapper.eq(Dish::getStatus,1);
        //添加排序条件
        lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> list = dishService.list(lambdaQueryWrapper);

        //使用lambada表达式转换dishDto中的records属性
        List<DishDto> dishDtoRecords = (list).stream().map((item) -> {
            DishDto dishDto = new DishDto();
            //将dish中的属性复制到dishDto中
            BeanUtils.copyProperties(item, dishDto);
            //获取Dish类中的categoryId属性
            Long categoryId = item.getCategoryId();
            //根据categoryId属性使用categoryService类查找菜品的category
            Category category = categoryService.getById(categoryId);

            if (category != null) {
                //根据category类中的getName方法找到categoryName
                String categoryName = category.getName();
                //将categoryName赋给dishDto中的CategoryName属性
                dishDto.setCategoryName(categoryName);

            }

            //添加口味数据
            Long id = item.getId();
            //构造条件查询器
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            //添加查询条件
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);
            List<DishFlavor> flavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper);

            dishDto.setFlavors(flavors);

            //返回新构造的dishDto
            return dishDto;
        }).collect(Collectors.toList());


        return R.success(dishDtoRecords);
    }
}



